package blt.app_core.utils;

import android.text.TextUtils;

import java.io.File;
import java.io.IOException;

/**
 * Created by Administrator on 2017/1/10.
 */
public class FileHelper {


    public static File getFile(String fileDir, String fileName, String url) {
        // check the file dir
        if (TextUtils.isEmpty(fileDir))
            throw new NullPointerException("File Dir is not null.");

        // make dir
        File dir = new File(fileDir);
        if (!dir.exists()) {
            dir.mkdirs();
        }

        // check the file name
        if (TextUtils.isEmpty(fileName)) {
            int separatorIndex = url.lastIndexOf("/");
            fileName = (separatorIndex < 0) ? url : url.substring(separatorIndex + 1, url.length());
            if (TextUtils.isEmpty(fileName) || !fileName.contains("."))
                fileName = String.valueOf(System.currentTimeMillis()) + ".cache";
        }

        return new File(dir, fileName);
    }

    public static File makeFile(File file) {
        if (file.exists()) {
            file.delete();
        }

        if (!file.exists()) {
            try {
                file.createNewFile();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return file;
    }

}
